package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kcs
 * @date 2021/11/25
 * @TIME:14:29
 */
@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品 dish(菜品表) dish_flavor(口味表)
     * @param dishDto
     * @return
     */
    @PostMapping
    public R addDish(@RequestBody DishDto dishDto){
        dishService.addDish(dishDto);
        return R.success("添加成功");
    }

    /**
     * 分页查询菜品信息
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R getByPage(Integer page,Integer pageSize,String name){
        IPage<DishDto> pageInfo= dishService.findByPage(page,pageSize,name);
        return R.success(pageInfo);
    }

    /**
     * 修改菜品信息 -- 回显
     * 根据菜品id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id){
        DishDto dishDto = dishService.get(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        //数据校验
        dishService.updateDish(dishDto);
        return R.success("修改成功");
    }

    /**
     * (批量)起售/停售状态修改
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("status/{status}")
    public R updateStatus(@PathVariable Integer status,Long[] ids){
        dishService.updateStatus(status,ids);
        return R.success("修改成功");
    }

    /**
     * 删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(Long[] ids){
        return dishService.delete(ids);
    }

    /**
     * 根据分类查询菜品
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R list(Dish dish){
        List<DishDto> dishDtos = dishService.getDishesByCategoryId(dish);
        return R.success(dishDtos);
    }

}
